<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.newsTitle" placeholder="文章标题" clearable style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.newsPosition" placeholder="文章类型" clearable style="width: 120px;" class="filter-item">
        <el-option v-for="item in positionList" :key="item.key" :label="item.value" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.newsDisplay" placeholder="状态" clearable style="width: 120px;" class="filter-item">
        <el-option :key="0" label="已撤下" :value="0" />
        <el-option :key="1" label="展示中" :value="1" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
      :default-sort="{prop: 'newsId', order: null}"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" fixed width="90px" prop="newsId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.newsId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章标题" align="center" min-width="200px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.newsTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column label="展示图片" align="center" width="90px">
        <template slot-scope="{row}">
          <el-image style="max-width: 50px; max-height: 28px" :src="row.newsImg" :preview-src-list="[row.newsImg]" />
        </template>
      </el-table-column>
      <el-table-column label="文章来源" align="center" width="150px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.newsSource }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任编辑" align="center" width="150px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.newsEditor }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" width="100px" prop="viewNum" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.viewNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转发量" align="center" width="100px" prop="repostNum" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.repostNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章类型" align="center" width="110px" prop="newsPosition" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" type="primary">{{ positionMap[row.newsPosition] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序权重" align="center" width="110px" prop="newsRank" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" effect="plain" type="primary"> {{ row.newsRank }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100px" prop="newsDisplay" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.newsDisplay===0" size="mini" effect="dark" type="info">已撤下</el-tag>
          <el-tag v-if="row.newsDisplay===1" size="mini" effect="dark" type="success">展示中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="最近修改时间" align="center" prop="updateTime" sortable="custom" width="140px">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="320px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            详情
          </el-button>
          <el-button type="primary" size="mini" @click="handleRank(row)">
            排序
          </el-button>
          <el-button v-if="row.newsDisplay===0" type="success" size="mini" @click="toggleUp(row.newsId)">
            展示
          </el-button>
          <el-button v-if="row.newsDisplay===1" type="info" size="mini" @click="toggleDown(row.newsId)">
            撤下
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.newsId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showNewsList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogVisible" width="30%">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-width="100px" style="width: 100%">
        <el-form-item label="版块名称" prop="sectionName">
          <el-input v-model="temp.sectionName" style="width: 90%" />
        </el-form-item>
        <el-form-item label="是否置顶" prop="sectionTop">
          <el-switch v-model="temp.sectionTop" :active-value="1" :inactive-value="0" active-color="#13ce66"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="设置排序权重" :visible.sync="dialogRankVisible" width="225px">
      <el-input-number v-model="tempRank.newsRank" :min="0" :step="1" step-strictly style="text-align: center" />
      <p style="color: gray">(权重数字越大，展示越靠前)</p>
      <p style="color: gray"></p>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogRankVisible = false">
          取消
        </el-button>
        <el-button size="small" type="primary" @click="setRank()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateRank, getNews, removeNews, showNewsList, toggleDisplay } from '@/api/news'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'Article',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: '',
        newsTitle: '',
        newsPosition: '',
        newsDisplay: undefined
      },
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      temp: {
        sectionId: undefined,
        sectionName: '',
        sectionTop: 0
      },
      tempRank: {
        newsId: undefined,
        newsRank: 0
      },
      rules: {
        sectionName: [{ required: true, message: '请填写版块名称', trigger: 'change' }],
        sectionTop: [{ required: true, message: '请设置是否置顶', trigger: 'change' }]
      },
      dialogRankVisible: false,
      dialogVisible: false,
      positionList: [
        { key: 0, value: '新闻资讯' },
        { key: 1, value: '信息资讯' },
        { key: 2, value: '维权保障信息' },
        { key: 3, value: '医疗互助信息' },
        { key: 4, value: '职工福利信息' }
      ],
      positionMap: {
        0: '新闻资讯',
        1: '信息资讯',
        2: '维权保障信息',
        3: '医疗互助信息',
        4: '职工福利信息'
      }
    }
  },
  created() {
    this.showNewsList()
  },
  methods: {
    showNewsList() {
      this.listLoading = true
      showNewsList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showNewsList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    resetDialog() {
      this.temp = {
        sectionId: undefined,
        sectionName: '',
        sectionTop: 0
      }
    },
    handleCreate() {
      this.$router.push({
        name: 'ArticleEdit',
        params: { id: 0 }
      })
    },
    handleUpdate(row) {
      this.$router.push({
        name: 'ArticleEdit',
        params: {
          id: row.newsId
        }
      })
    },
    toggleUp(newsId) {
      toggleDisplay({ newsId: newsId, newsDisplay: 1 }).then(() => {
        this.showNewsList()
        this.$notify({
          title: 'Success',
          message: '文章展示成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    toggleDown(newsId) {
      toggleDisplay({ newsId: newsId, newsDisplay: 0 }).then(() => {
        this.showNewsList()
        this.$notify({
          title: 'Success',
          message: '文章撤下成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    resetRankDialog() {
      this.tempRank = {
        newsId: undefined,
        newsRank: 0
      }
    },
    handleRank(row) {
      this.resetRankDialog()
      this.tempRank.newsId = row.newsId
      this.tempRank.newsRank = row.newsRank
      this.dialogRankVisible = true
    },
    setRank() {
      updateRank(this.tempRank).then(() => {
        this.showNewsList()
        this.dialogRankVisible = false
        this.$notify({
          title: 'Success',
          message: '设置排序权重成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleDelete(newsId) {
      this.$confirm('此操作将删除该文章，且不可逆，是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeNews(newsId).then(() => {
          this.showNewsList()
          this.$notify({
            title: 'Success',
            message: '删除文章成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    }
  }
}
</script>
